package com.ruoyi.web.controller.shopping;

import java.util.List;

import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.store.domain.Store;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.shopping.domain.MallCommissionWithdraw;
import com.ruoyi.shopping.service.IMallCommissionWithdrawService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 佣金提现审核Controller
 *
 * @author scx
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/shopping/withdraw")
public class MallCommissionWithdrawController extends BaseController
{
    @Autowired
    private IMallCommissionWithdrawService mallCommissionWithdrawService;

    /**
     * 查询佣金提现审核列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:withdraw:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallCommissionWithdraw mallCommissionWithdraw)
    {
        startPage();
        List<MallCommissionWithdraw> list = mallCommissionWithdrawService.selectMallCommissionWithdrawList(mallCommissionWithdraw);
        return getDataTable(list);
    }

    /**
     * 导出佣金提现审核列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:withdraw:export')")
    @Log(title = "佣金提现审核", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallCommissionWithdraw mallCommissionWithdraw)
    {
        List<MallCommissionWithdraw> list = mallCommissionWithdrawService.selectMallCommissionWithdrawList(mallCommissionWithdraw);
        ExcelUtil<MallCommissionWithdraw> util = new ExcelUtil<MallCommissionWithdraw>(MallCommissionWithdraw.class);
        return util.exportExcel(list, "佣金提现审核数据");
    }

    /**
     * 获取佣金提现审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:withdraw:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallCommissionWithdrawService.selectMallCommissionWithdrawById(id));
    }

    /**
     * 新增佣金提现审核
     */
    @PreAuthorize("@ss.hasPermi('shopping:withdraw:add')")
    @Log(title = "佣金提现审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallCommissionWithdraw mallCommissionWithdraw)
    {
        return toAjax(mallCommissionWithdrawService.insertMallCommissionWithdraw(mallCommissionWithdraw));
    }

    /**
     * 修改佣金提现审核
     */
    @PreAuthorize("@ss.hasPermi('shopping:withdraw:edit')")
    @Log(title = "佣金提现审核通过", businessType = BusinessType.UPDATE)
    @PutMapping("/pass/{ids}")
    public AjaxResult pass(@PathVariable Long[] ids)
    {
        for (int i = 0; i < ids.length; i++) {
            MallCommissionWithdraw withdraw = mallCommissionWithdrawService.selectMallCommissionWithdrawById(ids[i]);
            withdraw.setAuditStatus(DicConstants.AUIT_STATE_PASS.getCode());
            withdraw.setUpdateBy(getUserId());
            Store store = new Store();
            withdraw.setStoreId(String.valueOf(store.getId()));
            withdraw.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
            mallCommissionWithdrawService.updateMallCommissionWithdraw(withdraw);
//            StoreUserRole storeUserRole = new StoreUserRole();
//            storeUserRole.setStoreUserId(withdraw.getCreateBy());
//            storeUserRole.setStoreId(store.getId());
//            storeUserRole.setCreateBy(getUserId());
        }
        return toAjax(1);
    }
    /**
     * 拒绝佣金提现审核
     */
    @PreAuthorize("@ss.hasPermi('shopping:withdraw:edit')")
    @Log(title = "佣金提现审核", businessType = BusinessType.UPDATE)
    @PutMapping("/refuse")
    public AjaxResult refuse(@RequestBody MallCommissionWithdraw mallCommissionWithdraw)
    {
        mallCommissionWithdraw.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        mallCommissionWithdraw.setUpdateBy(getUserId());
        mallCommissionWithdraw.setAuditStatus(DicConstants.AUIT_STATE_REFUSE.getCode());
        return toAjax(mallCommissionWithdrawService.updateMallCommissionWithdraw(mallCommissionWithdraw));
        }

    /**
     * 删除佣金提现审核
     */
    @PreAuthorize("@ss.hasPermi('shopping:withdraw:remove')")
    @Log(title = "佣金提现审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallCommissionWithdrawService.logicDeleteMallCommissionWithdraw(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
